#!/usr/bin/python
#coding=utf-8

#python2.7
#
#凤凰小说网章节批量下载
#
#使用方法：
#将凤凰小说网内某部小说的章节列表页面的地址赋值给xiaoshuo_url变量，
#然后执行脚本即可。
#网址：http://www.fhxiaoshuo.com/
#
#xuwq
#2018-09-01

import sys
import re
import requests
from lxml import etree

reload(sys)  
sys.setdefaultencoding('utf8')   

#小说章节列表页地址
xiaoshuo_url = "http://www.fhxiaoshuo.com/read/24/24554/";

response = requests.get(xiaoshuo_url);
response.encoding='gbk';
dom_tree = etree.HTML(response.text);
chapter_list = dom_tree.xpath("//*[@id=\"list\"]/dl/dd[*]/a/@href");

for dir_link in chapter_list:
	
	#获取文章html页面
	response = requests.get(dir_link);
	response.encoding='gbk';
	dom_tree = etree.HTML(response.text);

	#获取文章标题
	#f.write();
	file_name = dom_tree.xpath("//*[@id=\"main\"]/div/div/div[2]/h1/text()")[0].encode('utf-8');
	f = open('./xiaoshuo/'+file_name+'.txt', 'w');

	#获取文章内容并写入文章中
	links = dom_tree.xpath("//*[@id=\"TXT\"]/text()");
	for text in links:
		f.write(text.encode('utf-8'));
		
	f.close();